package com.zwz.crm.ctl;

import com.zwz.crm.model.User;
import com.zwz.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * Created by zhaowuz on 2020/1/9.
 */
@Controller
public class AccountCtl {

    @Autowired
    private UserService userService;

    @Value("${bsi.isLogin}")
    public String isLogin;

    @RequestMapping("/")
    public String home() {
        return "redirect:/bsi/home";
    }

    @PostMapping("/login")
    public String login(Model model,  String account,  String pwd, HttpSession session) {
        if(!StringUtils.hasLength(account)|| !StringUtils.hasLength(pwd)){
            return "signIn";
        }
        User user = userService.checkUser(account, pwd);
        if (null == user) {
            model.addAttribute("msg", "账号或者密码错误");
            return "signIn";
        }else {
            session.setAttribute("isLogin",isLogin);
            session.setAttribute("user",user);
            return "redirect:/bsi/home";
        }
    }

    @GetMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("isLogin");
        session.removeAttribute("user");
        return "signIn";
    }

}
